package com.wyh.yuhcloud.service;


import com.wyh.yuhcloud.dto.ResultDto;
import com.wyh.yuhcloud.entity.SecurityUser;
import com.wyh.yuhcloud.entity.User;
import com.wyh.yuhcloud.entity.UserLogin;
import com.wyh.yuhcloud.util.EncrUtil;
import com.wyh.yuhcloud.util.RedisUtils;
import com.wyh.yuhcloud.util.TokenManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.Map;

/**
 * 登录校验方法
 *
 * @author ruoyi
 */
@Slf4j
@Transactional
@Component
public class SysLoginService
{
    @Autowired
    private EncrUtil encrUtil;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private RedisUtils redisUtils;


    /**
     * 登录验证
     *
     * @param username 用户名
     * @param password 密码
     * @return 结果
     */
    public ResultDto login(String username, String password, Integer remember)
    {
        //TODO password待解密,解密后加盐，再去UsernamePasswordAuthenticationToken验证

        // 用户验证
        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        // 用户验证
        Authentication authentication = null;
        try
        {
            // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
            authentication = authenticationManager
                    .authenticate(new UsernamePasswordAuthenticationToken(username, password));
        }
        catch (BadCredentialsException e)
        {
            log.error("账号或者密码错误"+e);
            throw e;
        }
        log.info("用户:"+username+"登录成功================");
        SecurityUser loginUser = (SecurityUser) authentication.getPrincipal();
        UserLogin user = loginUser.getCurrentUserInfo();
        Map<String, String> map = new HashMap<>();
        String token = null;
        if (user != null) {
            String userName = user.getUserName();
            map.put("username", userName);
            map.put("userId", user.getUserId());
            if (remember == 1) {
                token = tokenManager.createToken(map,604800000);
                redisUtils.set(token, "access_token", 604800); //存一周
            } else {
                token = tokenManager.createToken(map,86400000);
                redisUtils.set(token, "access_token", 86400); //存一天
            }

            ResultDto resultDto = new ResultDto();
            resultDto.setToken(token);
            resultDto.setUserName(encrUtil.encrypt(userName));
            resultDto.setUserId(user.getUserId());

            return resultDto;
        }
        // 生成token
        return null;
    }
}
